package com.opc.utils;


import com.opc.constant.OpcConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author SEG
 */
public class TestMain {
    public final static Logger LOGGER = LoggerFactory.getLogger(TestMain.class);

    public static void main(String[] args) {
        OpcClient opcClient = new OpcClient();
        // 1.显示server上的opc server应用列表
        opcClient.showAllOPCServer(OpcConstants.IP, OpcConstants.USER, OpcConstants.PASSWORD, OpcConstants.DOMAIN);
        // 2.连接指定的opc server
        boolean ret = opcClient.connectServer(OpcConstants.IP, OpcConstants.CLSID, OpcConstants.USER, OpcConstants.PASSWORD, OpcConstants.DOMAIN);
        if (!ret) {
            System.out.println("connect opc server fail");
            return;
        }

        // 3.检查opc server上的检测点
        List<String> itemIdList = new ArrayList<>();
        itemIdList.add(OpcConstants.Item);
        ret = opcClient.checkItemList(itemIdList);
        if (!ret) {
            System.out.println("not contain item list");
            return;
        }

        // 4.注册回调
        opcClient.subscribe((observable, arg) -> {
            Result result = (Result) arg;
            LOGGER.info("update result itemId:{}, value:{}", result.getItemId(), result.getValue());
        });

        // 5.添加监听检测点的数据
        // client和server在不同网段，可以访问
        opcClient.syncReadObject(OpcConstants.Item, 10000);

        /**
         * TODO 问题
         * client和server在不同网段，访问失败，比如：server为10.1.1.132，该网段下面又连接了扩展路由器，192.168.1.x，client为192.168.1.100
         */
//        opcClient.asyncReadObject("TEST.FB", 500);

        // 延迟
//        delay(10 * 60 * 1000);
    }

    private static void delay(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
